package xyz.msj.hrm.feign.fallback;

import feign.hystrix.FallbackFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import xyz.msj.hrm.feign.client.RedisFeignClient;
import xyz.msj.hrm.util.AjaxResult;

import java.util.List;

/**
 * 兜底方法
 */
@Component
@Slf4j //日志
public class RedisFeignClientFallbackFactory implements FallbackFactory<RedisFeignClient> {
    @Override
    public RedisFeignClient create(Throwable throwable) {

        return new RedisFeignClient() {
            /**
             * 设置值
             * http://localhost:1070/redis/set?key=username&value=123
             *
             * @param key
             * @param value
             * @RequestParam 获取地址上面对象的值
             */
            @Override
            public AjaxResult set(String key, String value) {
                log.error("存储缓存失败");
                throwable.printStackTrace();

                return AjaxResult.me().setSuccess(false).setMessage("存储缓存失败！");
            }

            @Override
            public AjaxResult setex(String key, int seconds, String value) {
                log.error("存储缓存失败");
                throwable.printStackTrace();

                return AjaxResult.me().setSuccess(false).setMessage("存储缓存失败！");
            }

            /**
             * 获取值
             * http://localhost:1070/redis/get/key
             *
             * @param key
             * @RequestParam 获取地址上面对象的值
             */
            @Override
            public AjaxResult get(String key) {
                log.error("获取缓存失败");
                throwable.printStackTrace();
                return AjaxResult.me().setSuccess(false).setMessage("获取缓存失败！");
            }

            /**
             * 删除值
             * http://localhost:1070/redis/del/key
             *
             * @param key
             * @RequestParam 获取地址上面对象的值
             */
            @Override
            public AjaxResult del(String key) {
                log.error("删除缓存失败");
                throwable.printStackTrace();
                return AjaxResult.me().setSuccess(false).setMessage("删除缓存失败！请检查");
            }

            /**
             * hash 存储值
             * http://localhost:1070/redis/set?key=username&value=123
             *
             * @param key
             * @param filed
             * @param value
             * @RequestParam 获取地址上面对象的值
             */
            @Override
            public AjaxResult hset(String key, String filed, byte[] value) {
                log.error("删除缓存失败");
                throwable.printStackTrace();
                return AjaxResult.me().setSuccess(false).setMessage("删除缓存失败！请检查");
            }

            /**
             * hash 获取值
             * http://localhost:1070/redis/set?key=username&value=123
             *
             * @param key
             * @param filed
             * @RequestParam 获取地址上面对象的值
             */
            @Override
            public AjaxResult hget(String key, String filed) {
                log.error("课程促销获取缓存失败");
                throwable.printStackTrace();
                return AjaxResult.me().setSuccess(false).setMessage("课程促销获取缓存失败！请检查");
            }

            /**
             * hash 获取所有值
             * http://localhost:1070/redis/set?key=username&value=123
             *
             * @param key
             * @RequestParam 获取地址上面对象的值
             */
            @Override
            public List<byte[]> hvals(String key) {
                log.error("课程促销获取缓存失败");
                throwable.printStackTrace();
                return null;
            }
        };
    }
}
